package com.tanhua.server.controller;

import cn.hutool.core.convert.Convert;
import com.tanhua.server.service.QuanZiService;
import com.tanhua.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/comments")
public class QuanZiCommentController {
    @Autowired
    private QuanZiService quanZiService;

    @PostMapping
    public ResponseEntity<Void> saveComment(@RequestBody() Map<String, String> map) {
        try {
            if (this.quanZiService.saveComment(Convert.toStr(map.get("movementId")), Convert.toStr(map.get("comment")))) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * TODO 按照规范;这里应该直接返回PageResult对象的
     * 但是因为前端接收参数的时候解析格式使用的ResponseEntity;所以我们只有返回ResponseEntity
     *
     * @param publishId 其实是评论id!!!
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public ResponseEntity<PageResult> listComments(@RequestParam("movementId") String publishId
            , @RequestParam(value = "page", defaultValue = "1") Integer page
            , @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {

        try {
            PageResult pageResult = this.quanZiService.listComments(publishId, page, pageSize);
            if (pageResult != null) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 对评论点赞
     *
     * @param publishId 其实是评论的id;但是跟对圈子点赞完全一样,所以可以直接复用(上下级,自关联的关系)
     *                  对圈子点赞时: commentId 是评论的id      , publishId 是 圈子的id
     *                  对评论点赞时: commentId 是评论的评论的id , publishId 是 评论的id
     * @return
     */
    @GetMapping("{id}/like")
    public ResponseEntity<Integer> likeComment(@PathVariable("id") String publishId) {
        try {
            Integer likeCount = this.quanZiService.likeComment(publishId);
            if (likeCount != null) {
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 对评论取消点赞
     *
     * @param publishId
     * @return
     */
    @GetMapping("{id}/dislike")
    public ResponseEntity<Integer> disLikeComment(@PathVariable("id") String publishId) {
        try {
            Integer likeCount = this.quanZiService.disLikeComment(publishId);
            if (null != likeCount) {
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
